#ifndef INDEXER_HPP
#define INDEXER_HPP

#include <QtCore/QString>
#include <QtCore/QThread>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QFileSystemWatcher>
#include "mod/db/database.hpp"
#include "mod/config/ConfigManager.hpp"
#include "model/AudioFile.hpp"

enum ScanMode {
    SCAN_ALL,    //scan all collection
    SCAN_UPDATE, //check for update on file & new
    SCAN_NEW     //check for new files
};

/** @brief Scan audio files and add them to the database. It also monitors file changes if it is enabled.
 */
class Indexer : public QThread {
    Q_OBJECT
    private:
        int mode;
        Database *db;
        ConfigManager *cm;
        QFileSystemWatcher *fsw;
        bool monitor;
        /** @brief Search audio files in the given directory
         * @param dir Directory path
         */
        void search(QString dir);
        QString formats;
    public:
        /** @brief Initialize the indexer
         * @param d Database object used to put data
         * @param c Configuration object used to get collection paths and file constraints
         * @param monitor Enable or disable file monitoring
         */
        Indexer(Database *d, ConfigManager *c, bool monitor=false);
        ~Indexer();
        /// @brief Run the scan on a thread
        void run();
        void enableMonitoring();
        void disableMonitoring();
        void deleteMissing();
        /// @brief Remove from collection audio files that does not fit constraints (min/max time, formats)
        void removeMissmatch();
    public slots:
        /** @brief Action to do when the file system watcher emits a file change
         * @param path Filepath
         */
        void updateFile  (const QString & path);
        /** @brief Action to do when the file system watcher emits a directory change
         * @param path Directory path
         */
        void updateFolder(const QString & path);
};

#endif // INDEXER_HPP
